﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using MoneyManagerNET.Model;
using System.Collections.Specialized;
using System.Windows.Data;
using System.Collections;
using System.Windows.Input;
using MoneyManagerNET.Presentation.Infrastructure;
using MoneyManagerNET.Presentation.ViewModel.Base;

namespace MoneyManagerNET.Presentation.ViewModel
{
    public class StockAccountViewModel : ViewModelBase<Model.IStockAccount>
    {
        public string Name
        {
            get
            {
                return (ModelAs == null) ? "<all stocks>" : ModelAs.Name; 
            }
        }

        public decimal? Total { get { return ModelAs == null ? App.Storage.Select<IStockAccount>().Sum(stock=>stock.GetTotal()) : ModelAs.GetTotal(); } }
        public decimal? Invested { get { return ModelAs == null ? App.Storage.Select<IStockAccount>().Sum(stock => stock.GetInvested()) : ModelAs.GetInvested(); } }
        public decimal? GainLoss { get { return ModelAs == null ? App.Storage.Select<IStockAccount>().Sum(stock => stock.GetGainLoss()) : ModelAs.GetGainLoss(); } }

        INotifyCollectionChanged _investmentCollection;

        INotifyCollectionChanged InvestmentCollection
        {
            get { return _investmentCollection; }
            set { _investmentCollection = value; OnChange("InvestmentCollection"); Investments = new CollectionView((IEnumerable)value); }
        }


        CollectionView _investmentd;
        public CollectionView Investments
        {
            get { return _investmentd; }
            set { _investmentd = value; OnChange("Investments"); }
        }

        public override object Model
        {
            get
            {
                return base.Model;
            }
            set
            {
                base.Model = value;
                IEnumerable<IStockInvestment> stockInvestments = (ModelAs == null) ? App.Storage.Select<IStockInvestment>() : ModelAs.StockAcquirements;//didnt understand else 
                InvestmentCollection = new ObservableCollection<IStockInvestment>(stockInvestments);
            }
        }

        public IStockInvestment Selected { get { return Investments == null?null: Investments.CurrentItem as IStockInvestment; } set { Investments.MoveCurrentTo(value); } }

        protected void PromptNew()
        {
            IStockInvestment inv =App.Storage.New<IStockInvestment>();
            inv.HeldAt = ModelAs;
            StockInvestmentViewModel vm = new StockInvestmentViewModel();
            vm.ModelAs = inv;

            var r = vm.ShowDialog();
            if (r.HasValue && r.Value)
            {
                var coll = InvestmentCollection as ICollection<IStockInvestment>;
                if (coll != null) coll.Add(inv);

                //TODO: do this in collections on change event

                OnChange("Total");
                OnChange("Invested");
                OnChange("GainLoss");

            }

        }
        public ICommand NewCommand { get; protected set; }
        protected void PromptEdit()
        {
            var investment = this.Selected as IStockInvestment;
            if (investment != null)
            {
                StockInvestmentViewModel vm = new StockInvestmentViewModel();
                vm.ModelAs = investment;
                vm.CanPromptHeldAt = investment != null;
                var r = vm.ShowDialog();
                if (r.HasValue && r.Value)
                {

                    //TODO: do this in collections on change event
                    OnChange("Total");
                    OnChange("Invested");
                    OnChange("GainLoss");
                }
            }
        }
        public ICommand EditCommand { get; protected set; } 

        protected void PromptDelete()
        {
            throw new NotImplementedException();
        }
        public ICommand DeleteCommand { get; protected set; }

        public StockAccountViewModel()
        {
            NewCommand = new DelegateCommand(o=>PromptNew());
            EditCommand = new DelegateCommand(o => PromptEdit());
            DeleteCommand = new DelegateCommand(o=>PromptDelete());
        }
    }

}